﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCtrl : CharacterBase
{
    private Vector3 eulerAngel;
    private Vector3 moves;
    private float speed = 3;
    private CharacterController characterCtrl;

    void Awake()
    {
        Bind(CharacterEvent.MOVE);
        characterCtrl = GetComponent<CharacterController>();
    }

    public override void Excute(int eventCode, object message)
    {
        switch (eventCode)
        {
            case CharacterEvent.MOVE:
                Debug.Log("角色开始移动了");
                Move((Vector2)message);
                break;
            default:
                break;
        }
    }

    /// <summary>
    ///  角色移动
    /// </summary>
    /// <param name="direction"></param>
    public void Move(Vector2 direction)
    {
        //旋转
        float angle = Mathf.Atan2(direction.x, direction.y) * Mathf.Rad2Deg;
        this.eulerAngel.y = angle;
        transform.rotation = Quaternion.Euler(eulerAngel);

        //移动
        float speedX = Mathf.Abs(direction.x);
        float speedY = Mathf.Abs(direction.y);

        float tempSpeed = Mathf.Sqrt(speedX * speed + speedY * speedY);
        moves.x = direction.x;
        moves.y = 0;
        moves.z = direction.y;

        characterCtrl.SimpleMove(moves * speed);

    }

}
